package org.chen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.domain.R;
import org.chen.domain.entity.VideoGenerationRecord;
import org.chen.domain.entity.VideoTemplate;
import org.chen.domain.request.VideoGenerationRequest;
import org.chen.domain.vo.TemplateQuery;
import org.chen.domain.vo.VideoRecordQuery;
import org.chen.service.TemplateService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/template")
@RequiredArgsConstructor
@Slf4j
public class TemplateController {
    private final TemplateService templateService;
    
    /**
     * 同步模板
     */
    @GetMapping("/sync")
    public R<String> syncTemplates() {
        try {
            return R.success(templateService.syncTemplates());
        } catch (Exception e) {
            log.error("同步模板失败", e);
            return R.error("同步模板失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取模板列表
     */
    @GetMapping("/list")
    public R<Page<VideoTemplate>> listTemplates(TemplateQuery query) {
        try {
            return R.success(templateService.listTemplates(query));
        } catch (Exception e) {
            log.error("获取模板列表失败", e);
            return R.error("获取模板列表失败");
        }
    }
    
    /**
     * 获取模板详情
     */
    @GetMapping("/{id}")
    public R<VideoTemplate> getTemplateDetail(@PathVariable String id) {
        try {
            return R.success(templateService.getTemplateDetail(id));
        } catch (Exception e) {
            log.error("获取模板详情失败", e);
            return R.error("获取模板详情失败");
        }
    }
    
    /**
     * 生成视频
     */
    @PostMapping("/generate")
    public R<VideoGenerationRecord> generateVideo(@RequestBody VideoGenerationRequest request) {
        try {
            return R.success(templateService.generateVideo(request));
        } catch (Exception e) {
            log.error("生成视频失败", e);
            return R.error("生成视频失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询视频状态
     */
    @GetMapping("/video/status/{recordId}")
    public R<VideoGenerationRecord> checkVideoStatus(@PathVariable String recordId) {
        try {
            return R.success(templateService.checkVideoStatus(recordId));
        } catch (Exception e) {
            log.error("查询视频状态失败", e);
            return R.error("查询视频状态失败");
        }
    }
    
    /**
     * 获取生成历史记录
     */
    @GetMapping("/video/history")
    public R<Page<VideoGenerationRecord>> getGenerationHistory(VideoRecordQuery query) {
        try {
            return R.success(templateService.getGenerationHistory(query));
        } catch (Exception e) {
            log.error("获取生成历史失败", e);
            return R.error("获取生成历史失败");
        }
    }
    
    /**
     * 删除生成记录
     */
    @DeleteMapping("/video/record/{recordId}")
    public R<Void> deleteGenerationRecord(@PathVariable String recordId) {
        try {
            templateService.deleteGenerationRecord(recordId);
            return R.success();
        } catch (Exception e) {
            log.error("删除生成记录失败", e);
            return R.error("删除生成记录失败");
        }
    }
}